package org.lmj.dos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.ImmutableMap;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.Category1Entity;
import org.lmj.dos.mapper.Category1Mapper;
import org.lmj.dos.mapper.Category2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    Category1Mapper category1Mapper;
    @Autowired
    Category2Mapper category2Mapper;

    @RequestMapping("/getAll")
    public ServerResponse getAll(){
        QueryWrapper<Category1Entity> wrapper1 = new QueryWrapper<>();
        List<Category1Entity> category1Entities = category1Mapper.selectList(wrapper1);
        List<Category1Entity> category1Res = category1Entities.stream().map(cat1Entity -> {
            cat1Entity.setCatalog2List(
                    category2Mapper.selectByMap(ImmutableMap.of("category1_id", cat1Entity.getId())));
            return cat1Entity;
        }).collect(Collectors.toList());
        return ServerResponse.success(category1Res);
    }
}
